package com.base.cn.platform.os.controller.order.order;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.order.order.ShopCart;
import com.base.cn.platform.os.service.course.book.BookService;
import com.base.cn.platform.os.service.order.order.ShopCartBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 购物车接口
 *
 * @author s.li
 * @create 2018-06-15-11:26
 */
@RestController
@RequestMapping("/manage/order")
public class ShopCartController extends BaseController {

    @Autowired
    private ShopCartBiz shopCartBiz;
    @Autowired
    private BookService bookService;

    /**
     * 删除购物车
     * @param shopCartIds 购物ID串
     */
    @RequestMapping("/deleteShopCart")
    public void deleteShopCart(@RequestParam("shopCartIds") String shopCartIds){
        shopCartBiz.deleteShopCartIds(shopCartIds);
    }

    /**
     * 修改购物车购买量
     * @param shopCartId 购物车ID
     * @param buyNum 购买量
     * @return
     */
    @RequestMapping("/updateShopCartNum")
    public Map<String,Object> updateShopCartNum(@RequestParam("shopCartId")BigDecimal shopCartId,
                                         @RequestParam("buyNum") int buyNum){
        ShopCart shopCart = shopCartBiz.findById(shopCartId);
        if(ObjectUtils.isEmpty(shopCart)){
            return ResultUtil.ERROR("购物车记录不存在");
        }
        if(!shopCart.getShopType().equals(ShopType.ORDER_SHOP_BOOK)){
            return ResultUtil.ERROR("只有图书类型商品可修改购买量");
        }
        Map<String,Object> book = bookService.findBookById(shopCart.getShopId(),false,false);
        int stockNum = Integer.parseInt(book.get("stockNum").toString());
        if(buyNum > stockNum){
            return ResultUtil.ERROR("图书库存不足");
        }
        if(buyNum <= 0){
            buyNum = 1;
        }
        shopCart.setBuyNum(buyNum);
        //执行修改
        shopCartBiz.updateById(shopCart,null);
        return ResultUtil.SUCCESS("修改成功");
    }

    /**
     * 添加购物车
     * @param userId 用户ID
     * @param shopId 商品ID
     * @param shopType 商品类型
     * @param buyNum 购买量，默认1
     * @param shareKey 推广标识
     * @return
     */
    @RequestMapping("/addShopCart")
    public Map<String,Object> addShopCart(@RequestParam("userId") BigDecimal userId,
                                          @RequestParam("shopId") BigDecimal shopId,
                                          @RequestParam("shopType") String shopType,
                                          @RequestParam(value = "buyNum",defaultValue = "1") int buyNum,
                                          @RequestParam(value = "shareKey",required = false) String shareKey){
        ShopCart shopCart = new ShopCart();
        shopCart.setUserId(userId);
        shopCart.setShopId(shopId);
        shopCart.setShopType(shopType);
        shopCart.setBuyNum(buyNum);
        shopCart.setShareKey(shareKey);
        return shopCartBiz.addShopCart(shopCart);
    }

    /**
     * 查询用户的购物车数据列表
     * @param userId 用户ID
     * @return List<ShopCart>
     */
    @RequestMapping("/findWebShowShopCartList")
    public List<ShopCart> findWebShowShopCartList(@RequestParam("userId") BigDecimal userId){
        return shopCartBiz.findWebShowShopCartList(userId);
    }

    /**
     * 查询用户购物车商品数量
     * @param userId 用户id
     * @return 购物车中商品数量
     */
    @RequestMapping("/queryCartShopNum")
    public Integer queryCartShopNum(@RequestParam("userId") BigDecimal userId){
        return shopCartBiz.queryCartShopNum(userId);
    }
}
